package com.waimai.app.qa.support;
/*package com.tibco.loglogic.qa.unit.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.tibco.loglogic.qa.utils.common.Constants;
import com.tibco.loglogic.qa.utils.common.FileUtil;

import br.eti.kinoshita.testlinkjavaapi.TestLinkAPI;
import br.eti.kinoshita.testlinkjavaapi.constants.ExecutionType;
import br.eti.kinoshita.testlinkjavaapi.model.Build;
import br.eti.kinoshita.testlinkjavaapi.model.TestCase;
import br.eti.kinoshita.testlinkjavaapi.model.TestCaseStep;
import br.eti.kinoshita.testlinkjavaapi.model.TestPlan;
import br.eti.kinoshita.testlinkjavaapi.model.TestSuite;
import br.eti.kinoshita.testlinkjavaapi.util.TestLinkAPIException;

public class TestLinkCasesHandler {
	
	private static final List<String> currentList = new ArrayList<String>();
	private static final String filePathOfMapForTestcasesInTestlink = "src/test/resources/largeMap";
	private TestLinkAPI api = null;
	private URL testlinkURL = null;
	
	static {
		currentList.add("ApacheWebServer");
		currentList.add("BlueCoatSyslog");
		currentList.add("CheckPoint");
		currentList.add("CiscoACS");
		currentList.add("CiscoASA");
		currentList.add("CiscoFWSM");
		currentList.add("CiscoIOS");
		currentList.add("Other_Unix");
		currentList.add("JuniperJunOS");
		currentList.add("LogLogicAppliance");
		currentList.add("MicrosoftDHCP");
		currentList.add("MicrosoftExchange2007PopImap");
		currentList.add("MicrosoftExchange2007SmtpRecieve");
		currentList.add("MicrosoftExchange2007SmtpSend");
		currentList.add("MicrosoftExchange2007messagetrack");
		currentList.add("MicrosoftSQLServer");
		currentList.add("Microsoft_Windows");
		currentList.add("Microsoft_Windows_French");
		currentList.add("PaloAltoNetworks");
		currentList.add("Tibco_ActiveMatrixAdmin");
		currentList.add("Tibco_Administrator");
		currentList.add("Tibco_EMS");
		currentList.add("VMWare_ESX");
		currentList.add("VMWare_Orchestrator");
		currentList.add("VMWare_vCenter");
		currentList.add("VMWare_vCloudDirector");
		currentList.add("VMWare_vShieldEdge");
		currentList.add("ads");
		currentList.add("CiscoSecureACS");
		currentList.add("F5_TMOS");
		currentList.add("Sourcefire");
		currentList.add("Snort");
		currentList.add("Sidewinder");
		currentList.add("Juniper_RT_FLOW");
		currentList.add("RSA_ACE_Server");
		currentList.add("MicrosoftSQLServerApp");
		currentList.add("Symantec_EP");
		currentList.add("Tripwire");
	}
	
	public TestLinkCasesHandler() {
		try {
			testlinkURL = new URL(Constants.TestLink_RPC_URL);
		} catch (MalformedURLException mue) {
			mue.printStackTrace(System.err);
			System.exit(-1);
		}
		try {
			api = new TestLinkAPI(testlinkURL, Constants.TestLinkDevKey);
		} catch (TestLinkAPIException te) {
			te.printStackTrace(System.err);
			System.exit(-1);
		}
	}

	public void printCaseIDAndCaseSteps() {
		if ( (System.currentTimeMillis() - new File(filePathOfMapForTestcasesInTestlink).lastModified()) 
			< 86400000 ) {
			return;
		}
		
		List<Integer> suiteIDList = new ArrayList<Integer>();
		TestPlan testPlan = api.getTestPlanByName(Constants.TestLinkTestPlanName, Constants.TestLinkProjectName);
		TestSuite[] testSuites = api.getTestSuitesForTestPlan(testPlan.getId());
		
		for (TestSuite testSuite : testSuites) {
			for (String item : currentList) {
				if (testSuite.getName().replace("_", "").toLowerCase().
					contains(item.replace("_", "").toLowerCase())) {
					suiteIDList.add(testSuite.getId());
				}
			}
		}
		
		String content = "";
		for (int item : suiteIDList) {
			TestCase[] testCases = api.getTestCasesForTestSuite(item,
					true, null);
			for (TestCase testCase : testCases) {
				List<TestCaseStep> stepsForEachCase = api.getTestCaseByExternalId(
						testCase.getFullExternalId(), testCase.getVersion())
						.getSteps();
				if (stepsForEachCase.size() == 0) {
					continue;
				}
				content += testCase.getFullExternalId() + ":";
				for (TestCaseStep testCaseStep : stepsForEachCase) {
					content += testCaseStep.getActions().replaceAll("\\s+", "").toUpperCase();
				}
				content += "\n";
			}
		}
		
		try {
			FileUtil.write(content, filePathOfMapForTestcasesInTestlink);
		} catch (IOException e) {
			e.printStackTrace(System.err);
			System.exit(-1);
		}
	}
	
	public static String getCaseID(String condition) {
		try {
			String content = FileUtil.read(new File(filePathOfMapForTestcasesInTestlink));
			String[] lines = content.split("\\n");
			for (String line : lines) {
				int endIndex = line.indexOf("</P><P>3");
				if (endIndex > -1) {
					line = line.substring(0, endIndex);
				}
				
				line = line.replaceAll("<.*?>", "").replace("&QUOT;", "\"")
						.replace("&GT;", ">").replace("&LT;", "<")
						.replace("&NBSP;", "");
				if (line.endsWith(condition) || (line.contains(condition) && line.endsWith("INTHESEARCHSECTIONOFPORT9680GUI"))) {
					int index = line.indexOf(':');
					return line.substring(0, index);
				}
			}
		} catch (IOException e) {
			e.printStackTrace(System.err);
			return null;
		}
		return null;
	}
	
	public void setCaseToAutomated(String caseID) {
		TestCase testCase = api.getTestCaseByExternalId(caseID, 1);
		if (testCase.getExecutionType() != ExecutionType.AUTOMATED) {
			testCase.setExecutionType(ExecutionType.AUTOMATED);
		}
	}
	
	private String getBuildNumber() {
		Properties properties = new Properties();
		try {
			properties.load(new FileInputStream(new File("/home/toor/tibco/logu/1.1/zookeeper/conf/build.properties")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
		String timestamp = properties.getProperty("timestamp");
		String buildNumber = "";
		if (timestamp.indexOf('-') > -1) {
			buildNumber = timestamp.substring(0, timestamp.indexOf('-'));
		} else {
			buildNumber = timestamp;
		}
		return buildNumber;
	}
	
	public String createBuildNameForSpecifiedTestplan() {
		TestPlan testPlan = api.getTestPlanByName(Constants.TestLinkTestPlanName, Constants.TestLinkProjectName);
		String buildName = getBuildNumber();
		api.createBuild(testPlan.getId(), buildName, "");
		Build[] builds = api.getBuildsForTestPlan(testPlan.getId());
		for (Build build : builds) {
			if (build.getName().equals(buildName)) {
				return buildName;
			}
		}
		return "";
	}
}
*/